package com.kk.score.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.kk.score.dao.ConfigDao;
import com.kk.score.entity.ConfigEntity;
import com.kk.score.entity.DepartmentEntity;
import com.kk.score.entity.PositionEntity;
import com.kk.score.entity.vo.GradeVo;
import com.kk.score.service.*;
import com.kk.score.utils.SpringSecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class ViewController {

    @Autowired
    private DepartmentService departmentService;

    @Autowired
    private PositionService positionService;

    @Autowired
    private TaskService taskService;

    @Autowired
    private GradeService gradeService;

    @Autowired
    private ConfigService configService;

    @GetMapping("/login")
    public String login() {
        return "login";
    }

    @GetMapping("/")
    public ModelAndView index(ModelAndView m) {
        if (SpringSecurityUtils.isAdmin()) {
            m.setViewName("index");
        } else {
            m.setViewName("user");
        }
        m.addObject("name", SpringSecurityUtils.getUserName());
        return m;
    }

    @GetMapping("/show")
    public ModelAndView show(ModelAndView m) {
        if (SpringSecurityUtils.isAdmin()) {
            m.setViewName("index");
        } else if (SpringSecurityUtils.getIsGrade() == 0) {
            m.setViewName("user");
        } else if (SpringSecurityUtils.getIsGrade() == 1) {
            m.setViewName("userG");
        }
        m.addObject("name", "用户");
        return m;
    }

    @GetMapping("/departmentList")
    @PreAuthorize("hasAnyRole('ROLE_ADMIN')")
    public String departmentList() {
        return "departmentList";
    }

    @GetMapping("/positionList")
    @PreAuthorize("hasAnyRole('ROLE_ADMIN')")
    public String positionList() {
        return "positionList";
    }

    @GetMapping("/unitList")
    @PreAuthorize("hasAnyRole('ROLE_ADMIN')")
    public String unitList() {
        return "unitList";
    }

    @GetMapping("/score")
    @PreAuthorize("hasAnyRole('ROLE_USER')")
    public ModelAndView scoreList() {
        ModelAndView m = new ModelAndView();
        m.setViewName("score");
        Map<String, Object> map = new HashMap<>();
        map.put("page", "1");
        map.put("limit", "300");
        m.addObject("taskList", taskService.queryPage(map).getList());
        m.addObject("scoreText", configService.getOne(new LambdaQueryWrapper<ConfigEntity>().eq(ConfigEntity::getStatus, 0)).getContext());
        return m;
    }

    @GetMapping("/userGrade")
    @PreAuthorize("hasAnyRole('ROLE_USER')")
    public ModelAndView userGrade() {
        ModelAndView m = new ModelAndView();
        m.setViewName("userGrade");
        Map<String, Object> map = new HashMap<>();
        map.put("page", "1");
        map.put("limit", "300");
        List<?> list = gradeService.queryPage(map).getList();
        m.addObject("gradeList", list);
        m.addObject("userGradeText", configService.getOne(new LambdaQueryWrapper<ConfigEntity>().eq(ConfigEntity::getStatus, 1)).getContext());
        String s = null;
        for (Object o : list) {
            String os = (((GradeVo) o).getStatus());
            if (os.equals("0")){
                s = os;
            }
        }
        m.addObject("isViewSub", s);
        return m;
    }

    @GetMapping("/push")
    @PreAuthorize("hasAnyRole('ROLE_ADMIN')")
    public String push(HttpSession session) {
        session.setAttribute("unitList", departmentService.list(new LambdaQueryWrapper<DepartmentEntity>().eq(DepartmentEntity::getIs_unit, 1).eq(DepartmentEntity::getStatus, 1)));
        session.setAttribute("scoreText", configService.getOne(new LambdaQueryWrapper<ConfigEntity>().eq(ConfigEntity::getStatus, 0)).getContext());
        return "push";
    }

    @GetMapping("/pushGrade")
    @PreAuthorize("hasAnyRole('ROLE_ADMIN')")
    public String pushGrade(HttpSession session) {
        List<DepartmentEntity> list = departmentService.list(new LambdaQueryWrapper<DepartmentEntity>().eq(DepartmentEntity::getIs_unit, 1).eq(DepartmentEntity::getStatus, 1));
        session.setAttribute("unitGradeList", list);
        session.setAttribute("userGradeText", configService.getOne(new LambdaQueryWrapper<ConfigEntity>().eq(ConfigEntity::getStatus, 1)).getContext());
        int badC = Integer.parseInt(configService.getOne(new LambdaQueryWrapper<ConfigEntity>().eq(ConfigEntity::getStatus, 3)).getContext());
        int sC = Integer.parseInt(configService.getOne(new LambdaQueryWrapper<ConfigEntity>().eq(ConfigEntity::getStatus, 2)).getContext());
        session.setAttribute("bad",badC);
        session.setAttribute("s",sC);
        return "grade";
    }

    @GetMapping("/userList")
    @PreAuthorize("hasAnyRole('ROLE_ADMIN')")
    public String userList(HttpSession session) {
        session.setAttribute("poList", positionService.list(new LambdaQueryWrapper<PositionEntity>().eq(PositionEntity::getStatus, 1)));
        session.setAttribute("depList", departmentService.list(new LambdaQueryWrapper<DepartmentEntity>().eq(DepartmentEntity::getStatus, 1).eq(DepartmentEntity::getIs_unit, 0)));
        return "userList";
    }

}
